//
//  RegisterViewController.m
//  Fengshui
//
//  Created by are on 13-6-3.
//
//

#import "RegisterViewController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.userTextfield becomeFirstResponder];
    self.resendNum = 45;
     self.title = @"注册手机号";
    // Do any additional setup after loading the view from its nib.
}
-(void)daojishis
{
    self.resendNum = self.resendNum - 1;
    if (self.resendNum == 0) {
        
        [self.time invalidate];
        self.refresSendBtn.enabled = YES;
        self.refresSendBtn.titleLabel.text = @"重新发送";
        self.resendNum = 45;
        
    }
    else
    {
        self.refresSendBtn.titleLabel.text =  [NSString stringWithFormat:@"重新发送(%d)",self.resendNum];
    }
}
-(IBAction)nextStepBtnAction:(id)sender
{
    if ([self.userTextfield.text isEqualToString:@""]) {
        
        return;

    }else

    {
        if([self checkTel:self.userTextfield.text])
        {

            NSString *string = [NSString stringWithFormat:@"http://fengshui22.sinaapp.com/ios/SmsVerify.php?uphone_num=%@",self.userTextfield.text];
            NSURL *url = [NSURL URLWithString:string];
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
            request.shouldAttemptPersistentConnection = NO;
            [request startSynchronous];
            NSError *error = [request error];
            NSLog(@"error:%@",error);
            NSString *strings = [request responseString];
            NSLog(@"string:%@",strings);
            if (!error) {

                NSString *string = [request responseString];
                if ([string isEqualToString:@"wrong_format"]) {

                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"格式错误" message:nil
                                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];

                }
                else if ([string isEqualToString:@"have_registered_before"])
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"已经注册过了" message:nil
                                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];

                }
                else
                {
                    NSString *yZMstr = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                    self.yanzhengMa = [NSString stringWithString:yZMstr];
                    self.refresSendBtn.enabled = NO;
                    NSString *description = [NSString stringWithFormat:@"重新发送(%d)",self.resendNum];
                    self.refresSendBtn.titleLabel.text = description;
                    self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(daojishis) userInfo:nil repeats:YES];
                    
                    [self.userTextfield resignFirstResponder];
                    [self.view addSubview:self.yanzhengView];
                    [self.yanzhengView setFrame:CGRectMake(320, 0, 320, self.yanzhengView.frame.size.height)];
                    self.miaoshuLabel.text = [NSString stringWithFormat:@"已给手机%@发了一条短信，请输入短信中的数字验证码",self.userTextfield.text];
                    
                    [UIView animateWithDuration:0.3f animations:^(void){

                        [self.yanzhengView setFrame:CGRectMake(0, 0, 320, self.registerView.frame.size.height)];

                    }];

                    [self.yanzhengmaTestfield becomeFirstResponder];
                    
                }
            }

           
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"格式错误" message:nil
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
    }
   }
-(IBAction)huoquYanzhengmaAction:(id)sender
{
    //获取uphone_num=13911762456用户的信息
    //http://fengshui22.sinaapp.com/ios/SmsVerify.php?uphone_num=13911762456
//    if ([self.userTextfield.text isEqualToString:@""]) {
//        
//        return;
//
//    }else
//
//    {
//        if([self checkTel:self.userTextfield.text])
//        {
            self.refresSendBtn.enabled = NO;
            NSString *description = [NSString stringWithFormat:@"重新发送(%d)",self.resendNum];
            self.refresSendBtn.titleLabel.text = description;
            self.time = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(daojishis) userInfo:nil repeats:YES];

            NSString *string = [NSString stringWithFormat:@"http://fengshui22.sinaapp.com/ios/SmsVerify.php?uphone_num=%@",self.userTextfield.text];
            NSURL *url = [NSURL URLWithString:string];
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
            [request startSynchronous];
            NSError *error = [request error];
            if (!error) {

                NSString *string = [request responseString];
                if ([string isEqualToString:@"wrong_format"]) {

                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"格式错误" message:nil
                                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];

                }
                else if ([string isEqualToString:@"have_registered_before"])
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"已经注册过了" message:nil
                                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];

                }
                else
                {
                    NSString *yZMstr = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                    self.yanzhengMa = [NSString stringWithString:yZMstr];
                }
            }

        
//        else
//        {
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"格式错误" message:nil
//                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//            [alert show];
//            [alert release];
//        }
//    }

}
-(IBAction)bangdinShoujihao:(id)sender
{

    if ([self.yanzhengmaTestfield.text isEqualToString:@""]) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"填写验证码" message:nil
                                                       delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];

    }
    else
    {
        NSString *string = [self.yanzhengmaTestfield.text encodeMd5];
        NSLog(@"%@",string);
        if ([string isEqualToString:self.yanzhengMa]) {

            NSString *string = @"http://fengshui22.sinaapp.com/ios/User.php";
            NSURL *url = [NSURL URLWithString:string];
            ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
            [request setPostValue:@"phone" forKey:@"submit_type"];
            [request setPostValue:self.userTextfield.text forKey:@"uphone_num"];
            [request setDelegate:self];
            [request startSynchronous];

            NSLog(@"%@",request.responseString);
            NSError *error = [request error];
            if (!error) {

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"绑定成功!" message:nil
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                [alert release];

                NSString *string = [NSString stringWithString:request.responseString];
                NSDictionary *dic = [string JSONValue];
                NSString *uid = [dic objectForKey:@"uid"];
                NSString *uphone_num = [dic objectForKey:@"uphone_num"];
                //                "uid": "2",
                //                "uname": "胡亮",
                //                "uphone_num": "13911762456",
                //                "usex": "1",
                //                "uborth_date": "320833500",
                //                "unow_area": "北京市丰台区",
                //                "uborth_area": "北京市朝阳区"
                NSDictionary *user_dic = [NSDictionary dictionaryWithObjectsAndKeys:uid,@"uid",uphone_num,@"uphone_num",nil];
                [[NSUserDefaults standardUserDefaults] setObject:user_dic forKey:@"userIphoneInfo"];
                [[NSUserDefaults standardUserDefaults] synchronize];

                [self dismissModalViewControllerAnimated:YES];
            }

        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"验证码错误请重新输入或获取" message:nil
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
        
    }
}
- (BOOL)checkTel:(NSString *)str
{
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    //    Dlog(@"phoneTest is %@",phoneTest);
    return [phoneTest evaluateWithObject:str];
}
#pragma ----
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.userTextfield resignFirstResponder];
    [self.yanzhengmaTestfield resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
